package com.imooc.example.order;

import com.imooc.example.order.command.StudentCreateCommand;
import com.imooc.example.order.query.StudentBean;
import org.axonframework.commandhandling.callbacks.LoggingCallback;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private CommandGateway commandGateway;

    @PostMapping
    public Object saveStudent(@RequestBody StudentBean studentBean) {
        UUID studentId = UUID.randomUUID();
        StudentCreateCommand command = StudentCreateCommand.builder()
                .studentId(studentId.toString())
                .name(studentBean.getName())
                .build();
        commandGateway.send(command, LoggingCallback.INSTANCE);

        return studentBean;
    }

}
